package com.ht.medicaltreatment.activity

import android.view.View
import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.ClickUtils
import com.blankj.utilcode.util.KeyboardUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.gym.baselib.base.BaseActivity
import com.ht.medicaltreatment.R
import com.ht.medicaltreatment.adapter.SampleReturnAdapetr
import com.ht.medicaltreatment.bean.CentrifugeSampleListBean
import com.ht.medicaltreatment.bean.IceboxItemListBean
import com.ht.medicaltreatment.view.CentrifugeProjectDialog
import com.ht.medicaltreatment.view.TaskDeleteDialog
import com.ht.medicaltreatment.view.UserNameDialog
import com.ht.medicaltreatment.viewmodel.ReturnViewModel
import com.xgst.lib.utils.TimeUtils
import com.xgst.lib.utils.showToast
import kotlinx.android.synthetic.main.activity_search_return.btnPositive
import kotlinx.android.synthetic.main.activity_search_return.btnStart
import kotlinx.android.synthetic.main.activity_search_return.cbAll
import kotlinx.android.synthetic.main.activity_search_return.etProjectName
import kotlinx.android.synthetic.main.activity_search_return.etRemark
import kotlinx.android.synthetic.main.activity_search_return.etSampleName
import kotlinx.android.synthetic.main.activity_search_return.imgProject
import kotlinx.android.synthetic.main.activity_search_return.llTop
import kotlinx.android.synthetic.main.activity_search_return.recyclerView
import kotlinx.android.synthetic.main.activity_search_return.tvCheckCount
import kotlinx.android.synthetic.main.activity_search_return.tvReturnName

/**
 * 归还 搜索样本 - 存入冰箱
 */
class SampleSearchReturnActivity : BaseActivity(), View.OnClickListener, OnItemClickListener {
    override fun getLayoutId() = R.layout.activity_search_return

    private val model by viewModels<ReturnViewModel>()
    private val adapter = SampleReturnAdapetr()
    private var userDialog: UserNameDialog? = null

    private val data: MutableList<IceboxItemListBean> = mutableListOf()
    private var id = ""
    private var itemAllCount = 0
    private var itemCheckCount = 0
    private var remarks = ""
    private var returnPerson = ""
    private var returnTableName = ""

    override fun init() {
        setTitle("订单列表")
        recyclerView.layoutManager = LinearLayoutManager(this)
        recyclerView.adapter = adapter

        initClick()
        adapter.setOnItemClickListener(this)

        ClickUtils.expandClickArea(cbAll, 70)
        model.projects {
            data.clear()
            val list = mutableListOf<IceboxItemListBean>()
            it.forEach { bean ->
                list.add(IceboxItemListBean(bean.id, bean.protocolNumber))
            }
            data.addAll(list)
        }
    }

    private fun initClick() {
        btnStart.setOnClickListener(this)
        etProjectName.setOnClickListener(this)
        imgProject.setOnClickListener(this)
        cbAll.setOnClickListener(this)
        btnPositive.setOnClickListener(this)
        llTop.setOnClickListener(this)
    }

    override fun onClick(v: View?) {
        when (v) {
            btnStart -> {
                val projectName = etProjectName.text.toString()
                val sampleName = etSampleName.text.toString()
                if (projectName.isEmpty() || sampleName.isEmpty()) {
                    showToast("请输入完整")
                    return
                }
                KeyboardUtils.hideSoftInput(etSampleName)
                showProgress()
                model.findSampleByName(id, sampleName, {
                    dismissProgress()
                    if (it.size > 0) {
                        btnPositive.visibility = View.VISIBLE
                    }else{
                        showToast("没有查询到相关数据")
                    }
                    itemAllCount = it.size
                    adapter.setNewInstance(it)
                }, {
                    dismissProgress()
                    showToast(it)
                })
            }

            etProjectName,
            imgProject -> {
                CentrifugeProjectDialog(this).show(data, { name, id ->
                    etProjectName.text = name
                    this.id = "$id"
                })
            }

            cbAll -> {
                if (cbAll.isChecked) {
                    adapter.data.forEach {
                        it.isCheck = true
                    }
                    itemCheckCount = itemAllCount
                } else {
                    adapter.data.forEach {
                        it.isCheck = false
                    }
                    itemCheckCount = 0
                }
                tvCheckCount.text = "$itemCheckCount"
                adapter.notifyDataSetChanged()
            }

            //归还人
            llTop -> {
                if (userDialog == null) {
                    userDialog = UserNameDialog(this)
                }
                userDialog!!.show { account, psw ->
                    model.getUserName(account, psw) {
                        tvReturnName.text = it
                    }
                }
            }

            //存入冰箱
            btnPositive -> {
                if (itemCheckCount == 0) {
                    showToast("请选择试管")
                    return
                }

                if (tvReturnName.text.isEmpty()) {
                    showToast("请输入归还人姓名")
                    return
                }

                TaskDeleteDialog(this).showDialog("确认要归还吗？") {
                    returnPerson = tvReturnName.text.toString()
                    remarks = etRemark.text.toString()
                    returnTableName = "临床试验进入冰箱(归还)记录${TimeUtils.getCurrentTime("yyyyMMddHHmmss")}"
                    showProgress()
                    var ids = ""
                    adapter.data.forEach {
                        if (it.isCheck) {
                            if (ids.isEmpty()) {
                                ids = "${it.id}"
                            } else {
                                ids = "$ids,${it.id}"
                            }
                        }
                    }

                    model.returnRefrigerator(returnPerson, remarks, ids, returnTableName, {
                        dismissProgress()
                        showToast("归还成功")
                        finish()
                    }, {
                        dismissProgress()
                        showToast(it)
                    })
                }
            }
        }
    }

    override fun onItemClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
        val item = adapter.data[position] as CentrifugeSampleListBean
        if (item.isCheck) {
            item.isCheck = false
            if (cbAll.isChecked) {
                cbAll.isChecked = false
            }
            itemCheckCount--
        } else {
            item.isCheck = true
            itemCheckCount++
            if (itemCheckCount == itemAllCount) {
                cbAll.isChecked = true
            }
        }
        tvCheckCount.text = "$itemCheckCount"
        adapter.notifyItemChanged(position)
    }
}
